package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wit.witmg.entity.TEquip;
import com.wit.witmg.entity.TSupplier;
import com.wit.witmg.mapper.TSupplierMapper;
import com.wit.witmg.service.ITSupplierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author bob
 * @since 2025-01-14
 */
@Service
public class TSupplierServiceImpl extends ServiceImpl<TSupplierMapper, TSupplier> implements ITSupplierService {

    @Override
    public Object add(HttpServletRequest request, String json) {
        TSupplier t= JSON.parseObject(json,TSupplier.class);
        if(t.getId()>0){
            boolean bl=  updateById(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_200);
            }
        }else{
            t.setDate(new Date());
            boolean bl= save(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_504);
            }
        }
        return JsonResult.success(t);
    }

    @Override
    public Object get(HttpServletRequest request, Integer id,Integer tp,String lab, String oid, String bn) {
        //参数 ：bn和tp按模糊查询的方式 tp返回数组；oid部门编号，返回oid以参数开头并包含参数的数据；bn返回单条数据（前端支持二维码识别），id返回单条数据，常用于连表查询；
        QueryWrapper<TSupplier> qw=new QueryWrapper<>();
        if(id!=null){
            qw.eq("id",id);
            qw.last("limit 1");
            return JsonResult.success(getOne(qw));
        }
        qw.eq("tp",tp);
        if(bn!=null){
            qw.like("bn",bn);
        }
        if(lab!=null){
            qw.like("lab",lab);
        }
        if(oid!=null){
            qw.like("oid",oid);
        }
        return JsonResult.success(list(qw));
    }

    @Override
    public Object del(HttpServletRequest request, Integer id) {
        boolean bl= removeById(id);
        if(bl) {
            return JsonResult.success();
        }
        return JsonResult.error(ErrorCode.ERR_200);
    }
}
